package com.preguicoso.client.estabelecimento.cardapio;

import java.util.ArrayList;

import br.com.uol.pagseguro.domain.Item;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.InlineLabel;
import com.preguicoso.client.telaInicial.lightBox;
import com.preguicoso.shared.entities.CategoriaBean;
import com.preguicoso.shared.entities.ItemCardapioBean;
import com.google.gwt.user.client.ui.FocusPanel;

public class CardapioCategoria extends Composite {

	private static CardapioCategoriaUiBinder uiBinder = GWT
			.create(CardapioCategoriaUiBinder.class);
	@UiField InlineLabel categoria;
	@UiField FocusPanel focusPanel;
	@UiField HTMLPanel itens;
	boolean isCarregado=false;

	private final CardapioServiceAsync cardapioService = GWT
			.create(CardapioService.class);
	
	ArrayList<ItemCardapioBean> lista = new ArrayList<ItemCardapioBean>();

	interface CardapioCategoriaUiBinder extends
			UiBinder<Widget, CardapioCategoria> {
	}

	public CardapioCategoria() {
		initWidget(uiBinder.createAndBindUi(this));
	}
	
	CategoriaBean i = new CategoriaBean();

	public CardapioCategoria(CategoriaBean i) {
		initWidget(uiBinder.createAndBindUi(this));
		categoria.setText(i.getNome());
		this.i = i;
	}
	@UiHandler("focusPanel")
	void onFocusPanelClick(ClickEvent event) {
		//lightBox.getInstance().showLoading("Aguarde...");
		atualizarCategoria("");
	}
	

	public void atualizarCategoria(final String test) {
		if(isCarregado){
			itens.clear();
			for (ItemCardapioBean i : lista) {
				if(i.getNome().toLowerCase().contains(test.toLowerCase()))
					itens.add(new CardapioItem(i));
			}
			if(test.equals(""))
				focusPanel.setVisible(true);
			else
				focusPanel.setVisible(false);
			
				
			lightBox.getInstance().hide();
			if(itens.isVisible())
				itens.setVisible(false);
			else
				itens.setVisible(true);
		}else{
			lightBox.getInstance().showLoading("Aguarde...");
			String[] tokens = History.getToken().split("/");
			
			cardapioService.getItensCardapioByCategoria(Long.parseLong(tokens[1]),i.getNome(), new AsyncCallback<ArrayList<ItemCardapioBean>>() {
				
				@Override
				public void onSuccess(ArrayList<ItemCardapioBean> result) {
					lightBox.getInstance().hide();
					lista = result;
					isCarregado = true;
					itens.setVisible(false);
			
					atualizarCategoria(test);
					
				}
				
				@Override
				public void onFailure(Throwable caught) {
				}
			});
		}
	}
}
